package com.jic.market.impl.rest.discount.model;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionMemberGrade;
import com.jic.market.enums.DiscountDetailStatusEnum;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.response.rest.coupon.QueryCouponPersonResponse;
import com.jic.market.response.rest.discount.GiftDetail;
import lombok.Data;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yinhaijun
 * @date: 2020/4/27
 */
@Data
public class ConditionDiscountModel implements Serializable,BaseDiscountModel {
    private static final long serialVersionUID = -3079584674318319636L;

    private Long id;
    /** ---------源数据--------- */
    // 活动流水号
    private String promotionSerialNo;
    /**
     * 活动id
     */
    private String promotionId;
    /**
     * 活动code
     */
    private String promotionCode;
    // 是否包邮
    private Boolean parcelsFlag;
    /**
     * 活动类型
     */
    private FrontPromotionTypeEnum frontPromotionTypeEnum;
    // 状态
    private DiscountDetailStatusEnum discountDetailStatusEnum;
    /**
     * 活动创建时间
     */
    private Date promotionCreateTime;
    /**
     * 优惠总价
     */
    private BigDecimal promotionDiscountTotalAmount;

    private List<String> thirdSkuIdList;

    /**
     * 是否叠加应用，次数，-1否，n次数，叠加型活动专用,如满额
     */
    private Integer overlayAppNum;
    /**
     * 是否叠加应用,0否，1是,
     */
    private Boolean overlayAppFlag;
    // 渠道
    private String channelCodes;
    // 会员类型
    private String memTypes;
    // 等级
    private List<PromotionMemberGrade> promotionMemberGradeList;

    /** -----------特殊活动专用数据------------- */
    /**
     * 优惠劵专用
     */
    private QueryCouponPersonResponse couponInfo;


    /**
     * 优惠运费
     */
    private BigDecimal promotionDiscountFreightAmount;
    /**
     * 赠品
     */
    List<GiftDetail> giftDetailList;


    @Override
    public Integer getSort() {
        if (null != couponInfo) {
            return couponInfo.getCouponPlatform();
        }
        return 0;
    }

    public BigDecimal getActualTotalAmount(Map<String, GoodsDetailModel> goodsDetailModelMap) {
        BigDecimal actualTotalAmount = BigDecimal.ZERO;
        if(null != thirdSkuIdList && !thirdSkuIdList.isEmpty()) {
            for (String thirdSkuId : thirdSkuIdList) {
                GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
                if (null == goodsDetailModel.getFrontPromotionTypeEnum()
                    || goodsDetailModel.getFrontPromotionTypeEnum().getGroup().equals(4)) { //  只有第4组的计算结果才会影响
                    actualTotalAmount = Money.add(actualTotalAmount, goodsDetailModel.getTotalAmount());
                } else {
                    actualTotalAmount = Money.add(actualTotalAmount, goodsDetailModel.getTotalAmount());
                    actualTotalAmount = Money.subtract(actualTotalAmount, goodsDetailModel.getPromotionDiscountTotalAmount());
                }
            }
        }
        return actualTotalAmount;
    }

    public BigDecimal getActualTotalFreightAmount(Map<String, GoodsDetailModel> goodsDetailModelMap){
        BigDecimal actualTotalFreightAmount = BigDecimal.ZERO;
        if(null != thirdSkuIdList && !thirdSkuIdList.isEmpty()){
            for(String thirdSkuId : thirdSkuIdList){
                GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
                if(null != goodsDetailModel.getLogisticsFee()){
                    actualTotalFreightAmount = Money.add(actualTotalFreightAmount, goodsDetailModel.getLogisticsFee());
                    if(null != goodsDetailModel.getPromotionDiscountFreightAmount()){
                        actualTotalFreightAmount = Money.subtract(actualTotalFreightAmount, goodsDetailModel.getPromotionDiscountFreightAmount());
                    }
                }

            }
        }
        return actualTotalFreightAmount;
    }

    public void calActualTotalFreightAmount(Map<String, GoodsDetailModel> goodsDetailModelMap){
        if(null != thirdSkuIdList && !thirdSkuIdList.isEmpty()){
            for(String thirdSkuId : thirdSkuIdList){
                GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
                goodsDetailModel.calcuDiscountFreightAmount(parcelsFlag);
            }
        }
    }

    public void calActualTotalFreightAmount(Map<String, GoodsDetailModel> goodsDetailModelMap, BigDecimal discountTotalFreightAmount){
        if(null != thirdSkuIdList && !thirdSkuIdList.isEmpty()){
            for(String thirdSkuId : thirdSkuIdList){
                GoodsDetailModel goodsDetailModel = goodsDetailModelMap.get(thirdSkuId);
                goodsDetailModel.calcuDiscountFreightAmount(discountTotalFreightAmount);
            }
        }
    }

}








